import Vue from 'vue'
import Router from 'vue-router'
import Test3 from '../test3'
import Test4 from '../test4'

Vue.use(Router)

let routes = [
  // {
  //   path:'/',
  //   redirect: '/another/test'
  // },
  // {
  //   path:'/another',
  //   redirect: '/another/test'
  // },
  {
    name: 'test3',
    component: Test3,
    meta: {
      title: '测试333333333333'
    }
  },
  {
    name: 'test4',
    component: Test4,
    meta: {
      title: '测试4444444444444444444'
    }
  }
]
routes.forEach(route => {
  route.path = route.path || '/another/' + (route.name || '')
})
const router = new Router({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title;
  if (title) document.title = title;
  // if (!to.matched.length) {
  //   next({ path: '/404', redirect: to.fullPath });
  // }
  if (to.matched.some(r => r.meta.requireAuth)) {
    if (store.state.users.currentUser.UserToken) {
      next();
    }
    else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  }
  else {
    next();
  }

})


export default router